package com.example.user.controller;

import com.example.common.model.UserDTO;
import com.example.common.utils.JwtUtil;
import com.example.user.client.UserFeignClient;
import com.example.user.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@Tag(name = "鉴权模块", description = "鉴权相关接口")
public class AuthController {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private AuthService authService;


    @GetMapping("/login")
    @Operation(summary = "用户名密码进行鉴权",
            description = "根据用户名密码进行鉴权")
    public String login(@RequestParam String username, @RequestParam String password) {
        // 这里只是简单演示，实际应验证密码
        if ("admin".equals(username) && "123456".equals(password)) {
            return JwtUtil.generateToken(username);
        }
        return "Invalid credentials";
    }


    @GetMapping("/register")
    @Operation(summary = "用户名密码进行鉴权",
            description = "根据用户名密码进行鉴权")
    public String register(@RequestParam String username, @RequestParam String password) {
        // 这里只是简单演示，实际应验证密码
        String token = authService.register(username, password);

        return token;
    }




    @GetMapping("/getUserById")
    @Operation(summary = "查询用户",
            description = "获取用户信息根据ID")
    public UserDTO getUserById(@RequestParam String id) {
        return userFeignClient.getUserById(id);
    }


}
